package com.xmut.rc.service.impl;

import com.xmut.rc.common.utils.JwtUtil;
import com.xmut.rc.entity.RolePermission;
import com.xmut.rc.mapper.RolePermissionMapper;
import com.xmut.rc.entity.SysPermission;
import com.xmut.rc.entity.UserRole;
import com.xmut.rc.mapper.SysPermissionMapper;
import com.xmut.rc.mapper.UserRoleMapper;
import com.xmut.rc.service.SysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.rc.common.utils.PermissionUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 权限接口实现
 * @author: XS
 * @date: 2022/5/23
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<SysPermission> getAllPermission() {
        //全部权限信息
        List<SysPermission> sysPermissions = baseMapper.selectList(null);
        return PermissionUtil.getLevelPermission(sysPermissions);
    }

    @Override
    public List<SysPermission> getRolePermission(String roleId) {
        List<RolePermission> rolePermissions = rolePermissionMapper.getRolePermissionByRoleId(roleId);
        List<SysPermission> permissions = new ArrayList<>();

        if (rolePermissions != null && rolePermissions.size() > 0) {
            for (RolePermission rolePermission : rolePermissions) {
                SysPermission sysPermission = baseMapper.selectById(rolePermission.getPermissionId());
                permissions.add(sysPermission);
            }
        }
        return permissions;
    }

    @Override
    public List<SysPermission> getPermission(String token) {
        //如果token不为空，返回权限
        if (StringUtils.hasText(token)) {
            //保存权限信息，不重复
            HashSet<SysPermission> permissionSet = new HashSet<>();
            // 1. 从user_role表通过用户id查询所有的角色信息
            List<UserRole> userRoles = userRoleMapper.getUserRoleByUserId(JwtUtil.getUserIdByJwtToken(token));
            for (UserRole userRole : userRoles) {
                // 2.根据roleId从role_permission表查询出所有的permissionId
                List<RolePermission> rolePermissions = rolePermissionMapper.getRolePermissionByRoleId(userRole.getRoleId());
                for (RolePermission rolePermission : rolePermissions) {
                    // 3.根据permissionId查询permission信息
                    SysPermission sysPermission = this.getBaseMapper().selectById(rolePermission.getPermissionId());
                    permissionSet.add(sysPermission);
                }
            }
            List<SysPermission> sorted = permissionSet.stream().sorted(Comparator.comparing(SysPermission::getId)).collect(Collectors.toList());
            //1.获得该用户的全部权限
            // permissionSet（不重复）
            //2.递归封装层级权限菜单
            List<SysPermission> permission = PermissionUtil.getLevelPermission(sorted);
            return permission;
        }
        return null;
    }
}
